﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;

namespace Server
{
    class ServerBluetooth
    {
        private BluetoothRadio bluetoothRadio;
        private BluetoothListener bluetootListener;
        private Guid myGUID = new Guid("{49A71E27-BF83-440A-98CC-91EF4177723F}");
        private bool listening = true;
        private BluetoothClient bluetoothClient;
        private Stream stream;

        public delegate void GetMessageEventHandler(object sender, GetMessageEventArgs e);
        public event GetMessageEventHandler GetMessage;

        public class GetMessageEventArgs : EventArgs
        {
            public ServerMessage message;
            public GetMessageEventArgs(ServerMessage message)
            {
                this.message = message;
            }
        }

        private void OnGetMessage(GetMessageEventArgs e)
        {
            if (GetMessage != null)
            {
                GetMessage(this, e);
            }
        }


        public void setGetMessageEventHandler(GetMessageEventHandler getMessageEventHandler)
        {
            GetMessage += getMessageEventHandler;
        }


        public bool start()
        {
            bluetoothRadio = BluetoothRadio.PrimaryRadio;

            if (bluetoothRadio == null)
            {
                return false;
            }
            else
            {
                if (bluetoothRadio.Mode != RadioMode.Discoverable)
                {
                    bluetoothRadio.Mode = RadioMode.Discoverable;
                }
            }

            bluetootListener = new BluetoothListener(myGUID);
            Thread t = new Thread(new ThreadStart(ListenLoop));

            bluetootListener.Start();
            t.Start();

            return true;
        }

        private void ListenLoop()
        {
            while (listening)
            {
                try
                {
                    bluetoothClient = bluetootListener.AcceptBluetoothClient();
                    stream = bluetoothClient.GetStream();
                }
                catch
                {
                }
                while (listening)
                {
                    int data = -1;

                    data = stream.ReadByte();
                    if (data >= 0)
                    {
                        GetMessageEventArgs e = new GetMessageEventArgs(new ServerMessage((byte)data));
                        OnGetMessage(e);
                    }
                }
                try
                {
                    stream.Close();
                    bluetoothClient.Close();
                    stream = null;
                    bluetoothClient = null;
                }
                catch
                {
                }

            }

        }

        public void sengMessage(ServerMessage m)
        {
            if (bluetoothClient != null && stream != null)
            {
                if (m.information != null)
                {
                    stream.Write(m.information, 0, 2);
                    stream.Flush();
                }
            }
        }

        public void close()
        {
            listening = false;
            if (stream != null)
            {
                stream.Close();
                stream = null;
            }
            if (bluetootListener != null)
            {
                bluetootListener.Stop();
                bluetootListener = null;
            }
            if (bluetoothClient != null)
            {
                bluetoothClient.Close();
                bluetoothClient = null;
            }

        }
    }
}
